package com.common.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;

/**
 * 周月相关
 *
 * 参考资料
 * https://blog.csdn.net/qq_39187822/article/details/124375773
 */
public class DateUtil2 {


    /**
     * @Description TODO 获取本周的第一天或最后一天
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     */
    public static String getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst) {
        Date time;
        Calendar cal = Calendar.getInstance();
        if (isFirst) {
            cal.add(Calendar.WEEK_OF_MONTH, 0);
            cal.set(Calendar.DAY_OF_WEEK, 2);
        } else {
            cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
            cal.add(Calendar.DAY_OF_WEEK, 1);
        }
        time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * @Description TODO 获取本年的第一天或最后一天
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     */
    public static String getStartOrEndDayOfYear(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
        }
        return resDate.toString();
    }



    /**
     * 功能描述: TODO 获取上一周的开始时间和结束时间（周一为每周第一天）
     */
    public static Map<String, String> getLastWeekStartOrEndDayOfWeek() {
        Map<String, String> dayMap = new HashMap<>(16);
        String beginTime;
        String endTime;
        Calendar date = Calendar.getInstance(Locale.CHINA);
        date.setFirstDayOfWeek(Calendar.MONDAY);
        //周数减一，即上周
        date.add(Calendar.WEEK_OF_MONTH, -1);
        //日子设为周几开始
        date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        beginTime = DateUtil.SDF.format(date.getTime());
        date.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        endTime = DateUtil.SDF.format(date.getTime());
        dayMap.put("beginTime", beginTime);
        dayMap.put("endTime", endTime);
        return dayMap;
    }


    /**
     * 功能描述: 获取本月的第一天或最后一天
     * @Param: [today, isFirst: true 表示开始时间，false表示结束时间]
     * @return: java.lang.String
     */
    public static String getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        int length = month.length(today.isLeapYear());
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), month, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), month, length);
        }
        return resDate.toString();
    }

    /**
     * 功能描述: 获取上月开始日期和结束日期
     */
    public static Map<String, String> getLastStartOrEndDayOfMonth() {
        Map<String, String> dayMap = new HashMap<>(16);
        //获取当前日期
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        //设置为1号,当前日期既为本月第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = DateUtil.SDF.format(cal.getTime());
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        //设置为1号,当前日期既为本月第一天
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDay = DateUtil.SDF.format(cale.getTime());
        dayMap.put("beginTime", firstDay);
        dayMap.put("endTime", lastDay);
        return dayMap;
    }

    public static void main(String[] args) {
        System.out.println(getStartOrEndDayOfMonth(null, false));
        System.out.println(getStartOrEndDayOfMonth(null, true));

        System.out.println(getStartOrEndDayOfYear(null, true));
        System.out.println(getStartOrEndDayOfYear(null, false));

        System.out.println(getLastStartOrEndDayOfMonth().get("beginTime"));
        System.out.println(getLastStartOrEndDayOfMonth().get("endTime"));

        System.out.println(getStartOrEndDayOfWeek(null, false));
        System.out.println(getStartOrEndDayOfWeek(null, true));


        System.out.println(getLastWeekStartOrEndDayOfWeek().get("beginTime"));
        System.out.println(getLastWeekStartOrEndDayOfWeek().get("endTime"));
    }




}
